<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AdminOrderController extends Controller
{
	//用户订单
    public function getOrder(Request $request)
    {
    	$res = DB::table('orders')
    	->join('order_goods','order_goods.order_id','=','orders.id')
    	->join('useraddress','useraddress.id','=','orders.address_id')
    	->join('users','users.id','=','orders.user_id')
    	->select('orders.user_id','orders.order_code','order_goods.goods_id','order_goods.goods_name','useraddress.getman','useraddress.address','orders.status','orders.id')
        ->where(function($query)use($request){
            $query->where('order_code','like','%'.$request->input('keywords').'%'); })
    	->paginate($request->input('num',10));
        // ->get();
    	return view('order.admin',['res'=>$res,'request'=>$request]);
    }

    //用户订单修改状态
    public function getEdit($id)
    {
    	// dd($id);
    	$res = DB::table('orders')->where('id',$id)->first();
    	$status = $res->status;
    	return view('order.adminedit',['status'=>$status,'id'=>$id]);
    }
    //修改订单状态
    public function postEditstatus(Request $request)
    {
    	// var_dump($id);
    	// dd($request->input('id'));
    	$data['status'] = $request->input('status');
    	// dd($data);
    	$res = DB::table('orders')->where('id',$request->input('id'))->update($data);
    	// dd($res);
    	if($res){
    		return redirect('/admin/order/order');
    	}
    }
    //删除订单
    public function getDele($id)
    {
    	// echo $id;
    	$res = DB::table('orders')->where('id',$id)->delete();
    	$resg = DB::table('order_goods')->where('order_id',$id)->delete();
    	if($res && $resg)
    	{
    		return redirect('/admin/order/order');
    	}
    }
}
